<?php
define('API_KEY', '03d9c32c03c3436cb190a40eee93adb3');
define('SECRET', 'f5c8e5027ae8440b92b570c0c72f9c6f');
class Renren{
	public function checkRenren(){
		//如果没有cookie则跳到登陆页
		if(!isset($_COOKIE[API_KEY]))$this->redirect('/account/login');
		$renren[API_KEY]=$_COOKIE[API_KEY];
		$renren['user_id']=$_COOKIE[API_KEY."_user"];
		$renren['session_key']=$_COOKIE[API_KEY."_session_key"];
		$renren['secret']=$_COOKIE[API_KEY."_ss"];
		$renren['expires']=$_COOKIE[API_KEY."_expires"];
		if($renren['expires'] < microtime(true)){//1、把“<API_KEY>_expires”对应的Cookie值与当前系统时间比较，如果小于当前系统时间，说明用户Session Key已经过期，即可判断验证失败，算法结束，否则，进行下一步验证；
			return $false;
		}
		$checckkey = md5("expires=" . $renren['expires'] . "session_key=" . $renren['session_key'] . "ss=" . $renren['secret'] . "user=" . $renren['user_id'] . SECRET);
		
		if($checckkey !== $renren[API_KEY]){
			//不是人人网用户，cookie非法
			throw new CHttpException('登录非法','不是人人网用户，请您使用人人账号登陆!');
		}
		return $renren;
	}
	public function getInfo(){
		//获取用户在人人网的信息
		$xn = new Xiaonei(API_KEY,SECRET,$_COOKIE[API_KEY."_session_key"]);
		$params = array (
			"uids"	=> array($_COOKIE[API_KEY.'_user']),
			"fields"=> array('name','sex','email_hash','birthday','headurl','hometown_location')
		);
		return $xn->users('getInfo',$params);
	}
	public function registerNC($renren){
		//nc_user
		$user = new User('renren');
		$user->user_name = $renren['user']['name'];
		$user->password = $renren['user']['uid'];
		$user->email = $_COOKIE[API_KEY."_session_key"];
		$user->is_active = 1;
		$user->is_remember = 1;
		$user->user_type = 'renren';
		$user->avatar = $renren['user']['headurl'];
		if(!$user->save())return false;
		$userID = $user->primaryKey;
	
		//nc_profiles
		if(isset($result_array['user']['birthday'])){
			$birth = @explode('-',$renren['user']['birthday']);
			$birth_year = $birth[0];
			$birth_month = $birth[1];
			$birth_day = $birth[2];
		}
		$birth_year = $birth_year ? $birth_year : '';
		$birth_month = $birth_month ? $birth_month : '';
		$birth_day = $birth_day ? $birth_day : '';
		if(isset($result_array['user']['hometown_location'])){
			$proviencd = $renren['user']['hometown_location']['province'];
			$city = $renren['user']['hometown_location']['city'];
		}
		$proviencd = $proviencd ? $proviencd : '';
		$city = $city ? $city : '';
		if(isset($result_array['user']['sex'])){
			$gender = $renren['user']['sex'] == 1 ? 'man' : 'women';
		}
		$gender = $gender ? $gender : '';
		$register_time = $last_login_time = time();
		$last_login_ip = Yii::app()->request->userHostAddress;
		$sql2 = "insert into nc_profiles (user_id,register_time,last_login_time,last_login_ip,birth_year,birth_month,birth_day,proviencd,city,gender) VALUES ('$userID','$register_time','$last_login_time','$last_login_ip','$birth_year','$birth_month','$birth_day','$proviencd','$city','$gender')";
		if(!Yii::app()->db->createCommand($sql2)->execute()){
			User::model()->deleteByPk($userID);
			return false;
		}

		//抓取人人的图片
		$userIcon = NC::get_icon($userID);
		ob_start(); 
		readfile($renren['user']['headurl']); 
		$img = ob_get_contents(); 
		ob_end_clean(); 
		$size = strlen($img); 
		$fp2=@fopen($userIcon, "a"); 
		fwrite($fp2,$img); 
		fclose($fp2); 
		return $user->attributes;
	}
}